package com.android.jingweiapp.adapter;

import android.content.Context;
import android.os.Parcelable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.jingweiapp.R;
import com.android.jingweiapp.bean.LeaderBean;
import com.android.jingweiapp.bean.OrganizationBean;
import com.android.jingweiapp.util.Contacts;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 *
 */
public class NormalRecyclerViewAdapter extends RecyclerView.Adapter<NormalRecyclerViewAdapter.NormalTextViewHolder> implements View.OnClickListener {
    private final LayoutInflater mLayoutInflater;
    private ArrayList<Parcelable> beans;
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    private int type;

    public NormalRecyclerViewAdapter(Context context, ArrayList<Parcelable> leaderBeans, int type) {
        this.beans = leaderBeans;
        mLayoutInflater = LayoutInflater.from(context);
        this.type = type;
    }

    @Override
    public NormalTextViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mLayoutInflater.inflate(R.layout.item_text, parent, false);
        return new NormalTextViewHolder(view);
    }

    @Override
    public void onBindViewHolder(NormalTextViewHolder holder, int position) {
        if (type == Contacts.TYPE_ADAPTER.TYPE_LEADER) {
            holder.mTextView.setText(((LeaderBean) beans.get(position)).getName());
            holder.mTextView.setTag(beans.get(position));
            holder.mTextView.setOnClickListener(this);
        } else {
            holder.mTextView.setText(((OrganizationBean.DataEntity) beans.get(position)).getName());
            holder.mTextView.setTag(beans.get(position));
            holder.mTextView.setOnClickListener(this);
        }
    }

    @Override
    public int getItemCount() {
        return beans == null ? 0 : beans.size();
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public void onClick(View v) {
        mOnItemClickListener.onItemClick(v, (Parcelable) v.getTag());
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, Parcelable data);
    }

    public static class NormalTextViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.text_view)
        TextView mTextView;

        NormalTextViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);

        }
    }
}
